import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import { Toast } from 'vant';
import vuexPersist from "vuex-persist";
export default new Vuex.Store({
  state: {
    color: "red",
    list1: [],
  },
  mutations: {
    //十六进制颜色随机
    qie(state) {
      let r = Math.floor(Math.random() * 256);
      let g = Math.floor(Math.random() * 256);
      let b = Math.floor(Math.random() * 256);
      state.color = "#" + r.toString(16) + g.toString(16) + b.toString(16);
    },
    // 加入书架
    ADD(state, item) {
      var flag = false;
      state.list1.forEach((i) => {
        if (item.id == i.id) {
          flag = true;
          Toast({
            message: '书架中已有此书！！！',
            icon: 'smile-o',
          });
        }
      })
      if (flag == false) {
        state.list1.push(item)
        Toast({
          message: '加入书架成功！！！',
          icon: 'like-o',
        });
      }
    },
    // 标记为已看完
    CHANGE(state, i) {
      state.list1.forEach((item, index) => {
        if (index == i) {
          if (item.state == 0) {
            item.state = 1;
          } else {
            item.state = 0;
          }
        }
      });
    },
    // 再看一遍
    CANCEL(state, i) {
      state.list1.forEach((item, index) => {
        if (index == i) {
          if (item.state == 0) {
            item.state = 2;
          } else {
            item.state = 0;
          }
        }
      });
    },
    // 删除
    DEL(state, index) {
      state.list1.splice(index, 1)
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ],
})